package com.jc.gameconter.controller.port.in;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.CommonStr;
import com.jc.common.util.ExceptionUtil;
import com.jc.ds.model.UserOrder;
import com.jc.framework.enums.LogType;
import com.jc.framework.enums.OrderStatus;
import com.jc.framework.enums.OrderType;
import com.jc.framework.web.ResponseBaseModel;
import com.jc.gameconter.controller.BaseController;
import com.jc.service.api.Api;
import com.jc.service.api.ParamModel;
import com.jc.service.chain.ChainRequestModel;
import com.jc.service.userorder.UserOrderService;
import com.jc.service.vrt.VrtService;
import com.jc.service.vrt.model.VrtResult;

@Controller
@RequestMapping("api")
public class ApiController extends BaseController {
	
	@Autowired
	private Api api;
	
	@Autowired
	private UserOrderService userOrderService;
	
	@Autowired
	private VrtService vrtService;
	/***
	 * 
	* @Title: proess  外部系统主动调用OMS的统一API入口,里面包含了统一签名校验.
	* @Description: TODO
	* @param @param paramObject
	* @param @return
	* @return ResponseEntity<?>
	* @throws
	 */
	@RequestMapping(method = RequestMethod.POST , consumes = "application/json;charset=utf8")
	public @ResponseBody ResponseEntity<?> proess(@RequestBody ParamModel paramObject){
		
		try {
			ResponseEntity<?> ret = api.deal(paramObject);
			return ret;
		}catch (Exception e) {
			ResponseEntity<?> ret = new ResponseEntity<>(new ResponseBaseModel(29001,e.getMessage()), HttpStatus.OK);
			return ret;
		}
	}
	
	
	
	@RequestMapping(value="chaincall" , method = RequestMethod.POST)
	public @ResponseBody String chaincall(HttpServletRequest request,ChainRequestModel model){
		try {
			getLoger().error("接收到的区块连回调 -------- : ", model == null ? "" : JSONObject.toJSON(model));
			
			userOrderService.transferCallBack(model);			
			return CommonStr.SUCCESS;
		}catch (Exception e) {
			ResponseEntity<?> ret = new ResponseEntity<>(new ResponseBaseModel(29001,e.getMessage()), HttpStatus.OK);
			return "FAIL : " + ExceptionUtil.subExceptionMessage(e, 0, 100);
		}
	}
	
	
	/****
	 * 交易扫描转账, 
	 * @param request
	 * @param model
	 * @return
	 */
	
	@RequestMapping(value="bourse/query" , method = RequestMethod.POST)
	public @ResponseBody String query(HttpServletRequest request,ChainRequestModel model){
		try {
			
			UserOrder _uo = new UserOrder();
			_uo.setOrderStatus(OrderStatus.交易中.getIndex());
			_uo.setOrderType(OrderType.VRT提现.getIndex());
			
			
			
			return CommonStr.SUCCESS;
		}catch (Exception e) {
			ResponseEntity<?> ret = new ResponseEntity<>(new ResponseBaseModel(29001,e.getMessage()), HttpStatus.OK);
			return "FAIL : " + ExceptionUtil.subExceptionMessage(e, 0, 100);
		}
	}
	
	
	
	@RequestMapping(value="bourse/callback" , method = RequestMethod.POST)
	public @ResponseBody String callback(HttpServletRequest request,ChainRequestModel model){
		try {
			
			
			
			
			return CommonStr.SUCCESS;
		}catch (Exception e) {
			ResponseEntity<?> ret = new ResponseEntity<>(new ResponseBaseModel(29001,e.getMessage()), HttpStatus.OK);
			return "FAIL : " + ExceptionUtil.subExceptionMessage(e, 0, 100);
		}
	}
	
	
	
	
	@RequestMapping(value="postvrt" , method = RequestMethod.POST)
	public @ResponseBody String pushVRT(HttpServletRequest request,Map<String,Object> noticeMap){
		try {
			noticeMap = readParamToMap(request);
			VrtResult ret = vrtService.pushVrtClub(noticeMap, "/saveGameChargeLog", LogType.NOTICE_VRT_CLUB_LOGIN);
			//getLoger().error(JSONObject.toJSONString(noticeMap) + "----请求结果----------------------------> :   " + JSONObject.toJSONString(ret));
			return CommonStr.SUCCESS;
		}catch (Exception e) {
			ResponseEntity<?> ret = new ResponseEntity<>(new ResponseBaseModel(29001,e.getMessage()), HttpStatus.OK);
			return "FAIL : " + ExceptionUtil.subExceptionMessage(e, 0, 100);
		}
	}
	
	
	
	
}
